package com.pet.controller.admin;

import com.pet.constant.JwtClaimsConstant;
import com.pet.dto.admin.adminData.EmployeeDTO;
import com.pet.dto.web.EmployeeLoginDTO;
import com.pet.entity.Employee;
import com.pet.properties.JwtProperties;
import com.pet.result.Result;
import com.pet.service.EmployeeService;
import com.pet.utils.JwtUtil;
import com.pet.vo.system.adminData.EmployeeVO;
import com.pet.vo.web.EmployeeLoginVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/admin/employee")
@Slf4j
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 登录
     * l
     *
     * @param employeeLoginDTO
     * @return
     */
    @PostMapping("/login")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录：{}", employeeLoginDTO);

        Employee employee = employeeService.login(employeeLoginDTO);//进行登录判定

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();//这个HashMap的键（key）是String类型，值（value）是Object类型。
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());//添加员工id
        log.info("生成jwt令牌,claims：{}", claims);
        String token = JwtUtil.createJWT(//生成jwt令牌的操作类
                jwtProperties.getAdminSecretKey(),//生成相关变量属性
                jwtProperties.getAdminTtl(),
                claims);

        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();

        return Result.success(employeeLoginVO);
    }

    /**
     * 查询管理员信息
     */
    @GetMapping("/adminInfo")
    public Result<EmployeeVO> get() {
        log.info("获取当前登录管理员信息...");
        EmployeeVO employeeVO = employeeService.getAdminInfo();
        return Result.success(employeeVO);
    }

    /**
     * 修改管理员信息
     */
    @PutMapping("/adminInfo")
    public Result<String> update(@RequestBody EmployeeDTO employeeDTO) {
        log.info("修改管理员信息：{}", employeeDTO);
        employeeService.updateAdminInfo(employeeDTO);
        return Result.success();
    }

    /**
     * 删除管理员
     */

}
